package com.dataaccess;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;

import com.dataobject.MyReminder;

public class MyDatabase {
	private static final String TB_REMIND = "NHAC_NHO";
	private static final String C_ID = "MA";
	private static final String C_ICO = "BIEU_TUONG";
	private static final String C_TITLE = "TIEU_DE";
	private static final String C_CONTENT = "NOI_DUNG";
	private static final String C_DATETIME = "NGAY_GIO";
	private static final String C_ALARM = "KIEU_TB";
	private static final String C_TONE = "AM_TB";

	private Context context;
	private DatabaseHelper dbHelper;
	private SQLiteDatabase db;

	public MyDatabase(Context c) {
		this.context = c;
	}

	public MyDatabase open() throws SQLException {
		dbHelper = new DatabaseHelper(context);
		db = dbHelper.getWritableDatabase();
		return this;
	}

	public void close() {
		dbHelper.close();
	}

	public long insert(MyReminder mr) {
		ContentValues cv = new ContentValues();
		cv.put(C_ICO, mr.get_Ico());
		cv.put(C_TITLE, mr.get_Title());
		cv.put(C_CONTENT, mr.get_Content());
		cv.put(C_DATETIME, mr.get_DateTime().getTimeInMillis());
		cv.put(C_ALARM, mr.get_Alarm());
		cv.put(C_TONE, mr.get_Tone());
		return db.insert(TB_REMIND, null, cv);
	}

	public List<MyReminder> getAll() {
		Cursor c = db.query(TB_REMIND, new String[] { C_ID, C_ICO, C_TITLE,
				C_CONTENT, C_DATETIME, C_ALARM, C_TONE }, null, null, null,
				null, null);
		List<MyReminder> mrl = new ArrayList<MyReminder>();
		for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()) {
			MyReminder mr = new MyReminder(c.getInt(c.getColumnIndex(C_ID)),
					c.getString(c.getColumnIndex(C_ICO)), c.getString(c
							.getColumnIndex(C_TITLE)), c.getString(c
							.getColumnIndex(C_CONTENT)), c.getLong(c
							.getColumnIndex(C_DATETIME)), c.getString(c
							.getColumnIndex(C_ALARM)), c.getString(c
							.getColumnIndex(C_TONE)));
			mrl.add(mr);
		}
		c.close();
		return mrl;
	}

	public List<MyReminder> getPreviousList() {
		Cursor c = db.query(TB_REMIND, new String[] { C_ID, C_ICO, C_TITLE,
				C_CONTENT, C_DATETIME, C_ALARM, C_TONE }, null, null, null,
				null, null);
		List<MyReminder> mrl = new ArrayList<MyReminder>();
		for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()) {
			MyReminder mr = new MyReminder(c.getInt(c.getColumnIndex(C_ID)),
					c.getString(c.getColumnIndex(C_ICO)), c.getString(c
							.getColumnIndex(C_TITLE)), c.getString(c
							.getColumnIndex(C_CONTENT)), c.getLong(c
							.getColumnIndex(C_DATETIME)), c.getString(c
							.getColumnIndex(C_ALARM)), c.getString(c
							.getColumnIndex(C_TONE)));
			if (getDayNotTimeOfCalendar(mr.get_DateTime().getTime()).before(
					getDayNotTimeOfCalendar(Calendar.getInstance().getTime()))) {
				mrl.add(mr);
			}
		}
		c.close();
		return mrl;
	}

	public List<MyReminder> getTodayList() {
		Cursor c = db.query(TB_REMIND, new String[] { C_ID, C_ICO, C_TITLE,
				C_CONTENT, C_DATETIME, C_ALARM, C_TONE }, null, null, null,
				null, null);
		List<MyReminder> mrl = new ArrayList<MyReminder>();
		for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()) {
			MyReminder mr = new MyReminder(c.getInt(c.getColumnIndex(C_ID)),
					c.getString(c.getColumnIndex(C_ICO)), c.getString(c
							.getColumnIndex(C_TITLE)), c.getString(c
							.getColumnIndex(C_CONTENT)), c.getLong(c
							.getColumnIndex(C_DATETIME)), c.getString(c
							.getColumnIndex(C_ALARM)), c.getString(c
							.getColumnIndex(C_TONE)));
			if (getDayNotTimeOfCalendar(mr.get_DateTime().getTime()).equals(
					getDayNotTimeOfCalendar(Calendar.getInstance().getTime()))) {
				mrl.add(mr);
			}
		}
		c.close();
		return mrl;
	}

	public List<MyReminder> getNextList() {
		Cursor c = db.query(TB_REMIND, new String[] { C_ID, C_ICO, C_TITLE,
				C_CONTENT, C_DATETIME, C_ALARM, C_TONE }, null, null, null,
				null, null);
		List<MyReminder> mrl = new ArrayList<MyReminder>();
		for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()) {
			MyReminder mr = new MyReminder(c.getInt(c.getColumnIndex(C_ID)),
					c.getString(c.getColumnIndex(C_ICO)), c.getString(c
							.getColumnIndex(C_TITLE)), c.getString(c
							.getColumnIndex(C_CONTENT)), c.getLong(c
							.getColumnIndex(C_DATETIME)), c.getString(c
							.getColumnIndex(C_ALARM)), c.getString(c
							.getColumnIndex(C_TONE)));
			Calendar tmp = Calendar.getInstance();
			tmp.add(Calendar.DATE, 1);
			if (getDayNotTimeOfCalendar(mr.get_DateTime().getTime()).after(
					getDayNotTimeOfCalendar(tmp.getTime()))) {
				mrl.add(mr);
			}
		}
		c.close();
		return mrl;
	}

	public List<MyReminder> getTomorrowList() {
		Cursor c = db.query(TB_REMIND, new String[] { C_ID, C_ICO, C_TITLE,
				C_CONTENT, C_DATETIME, C_ALARM, C_TONE }, null, null, null,
				null, null);
		List<MyReminder> mrl = new ArrayList<MyReminder>();
		for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()) {
			MyReminder mr = new MyReminder(c.getInt(c.getColumnIndex(C_ID)),
					c.getString(c.getColumnIndex(C_ICO)), c.getString(c
							.getColumnIndex(C_TITLE)), c.getString(c
							.getColumnIndex(C_CONTENT)), c.getLong(c
							.getColumnIndex(C_DATETIME)), c.getString(c
							.getColumnIndex(C_ALARM)), c.getString(c
							.getColumnIndex(C_TONE)));
			Calendar tmp = Calendar.getInstance();
			tmp.add(Calendar.DATE, 1);
			if (getDayNotTimeOfCalendar(mr.get_DateTime().getTime()).equals(
					getDayNotTimeOfCalendar(tmp.getTime()))) {
				mrl.add(mr);
			}
		}
		c.close();
		return mrl;
	}

	private Calendar getDayNotTimeOfCalendar(Date date) {
		Calendar c = Calendar.getInstance();
		c.setTime(date);
		c.set(Calendar.HOUR_OF_DAY, 0);
		c.set(Calendar.MINUTE, 0);
		c.set(Calendar.SECOND, 0);
		c.set(Calendar.MILLISECOND, 0);
		return c;
	}

	public int deleteReminder(MyReminder mr) {
		return db.delete(TB_REMIND, C_ID + "=" + mr.get_Id(), null);
	}

	public int deleteAll() {
		return db.delete(TB_REMIND, null, null);
	}

	public boolean updateReminder(MyReminder mr) {
		ContentValues cv = new ContentValues();
		cv.put(C_ICO, mr.get_Ico());
		cv.put(C_TITLE, mr.get_Title());
		cv.put(C_CONTENT, mr.get_Content());
		cv.put(C_DATETIME, mr.get_DateTime().getTimeInMillis());
		cv.put(C_ALARM, mr.get_Alarm());
		cv.put(C_TONE, mr.get_Tone());
		long result = db.update(TB_REMIND, cv, C_ID + "='" + mr.get_Id() + "'",
				null);
		if (result == 0)
			return false;
		return true;
	}
}
